import numpy as np
import torch
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt


def f(x):
    return x * torch.cos(np.pi * x)


def g(x):
    return f(x) + 0.2 * torch.cos(5 * np.pi * x)


x = torch.arange(0.5, 1.5, 0.02)
fig = plt.figure(figsize=(4.5, 2.5))
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'
ax = fig.add_subplot()
# 风险函数：整个数据群的预期损失
ax.plot(x, f(x), 'r--', label="风险函数")
# 经验风险函数：训练数据集的平均损失
ax.plot(x, g(x), 'b-', label="经验风险函数")
plt.legend()
plt.show()
